#!/bin/bash

# Creates a .tar.gz file of current directory
# mkt
# File ID: dc74ffde-08f1-11de-a419-000475e441b9
# (C)opyleft 1999- sunny@sunbase.org
# License: GNU General Public License version 2 or later

# FIXME: Finn ut av -r kombinert med -n

if [ "x$1" = "x--help" -o "x$1" = "x-h" ]; then
	cat <<END

Usage: $0 [options] name_without_the_tar_gz_extension

Creates a .tar.gz file of current directory tree and places it in
parent directory. If nothing is specified, current dir is used.

Options:

-g, --gpg     Use gpg instead of gzip
-h, --help    Print this help message
-n, --nocomp  No compression, leave as .tar
-r, --remove  Remove directory after packing

Options has to be specified in alphabethical order at the moment.

END
	exit 1
fi

if [ "x$1" = "x--gpg" -o "x$1" = "x-g" ]; then
	_packcmd="gpg -e"
	_packext=".gpg"
	shift
else
	_packcmd="gzip -vN"
	_packext=".gz"
fi

if [ "x$1" = "x--nocomp" -o "x$1" = "x-n" ]; then
	_no_compress=1
	unset _packext
	shift
else
	_no_compress=0
fi

if [ "x$1" = "x--remove" -o "x$1" = "x-r" ]; then
	_can_erase=1
	shift
else
	_can_erase=0
fi

if [ $# -eq 0 ]; then
	_pwd=`pwd`
	_prefix=`basename $_pwd`
else
	_prefix=`basename $1`
fi

if [ -e ../$_prefix.tar -o -e ../$_prefix.tar$_packext ]; then
	echo $0: ../$_prefix.tar or ../$_prefix.tar$_packext already exists >&2
	exit 1
fi

if [ -e ../$_prefix -a ! ../$_prefix -ef . ]; then
	echo $0: ../$_prefix and current dir are not the same >&2
	exit 1
fi

if [ -e ../$_prefix -a ! -d ../$_prefix ]; then
	echo $0: ../$_prefix: Not a directory >&2
	exit 1
fi

if [ ! -e ../$_prefix ]; then
	cp -a . ../$_prefix || { echo "$0: Error running \"cp -a . ../$_prefix\"" >&2; exit 1; }
fi

if [ ! -d ../$_prefix ]; then
	echo $0: Unable to create directory ../$_prefix >&2
	exit 1
fi

HOSTN="`hostname`"
CURRDIR="`/bin/pwd`"
cd .. || { echo "$0: Cannot \"cd ..\"" >&2; exit 1; }
uuid=`suuid -m -t mkt --raw -w eo -c "<c_mkt> <filename>$_prefix.tar$_packext</filename> <host>$HOSTN</host> <directory>$CURRDIR</directory> </c_mkt>"` || { echo $0: suuid error >&2; exit 1; }
find $_prefix -print0 | LC_COLLATE=C sort -z | tar cf $_prefix.tar --null -T - --no-recursion --label=$uuid $_prefix || { echo "$0: Error running tar in parent dir" >&2; exit 1; }

if [ "$_no_compress" -eq "1" ]; then
	exit 0
fi

$_packcmd $_prefix.tar || { echo "$0: Error running \"$_packcmd $_prefix.tar\" in parent dir" >&2; exit 1; }

if [ "$_packext" = ".gpg" ]; then
	gitwipe $_prefix.tar
	sync
	rm -f $_prefix.tar
fi

if [ "$_can_erase" -eq "1" ]; then
	rm -rf $_prefix || { echo "$0: $_prefix: Can't remove directory" >&2; exit 1; }
fi

# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2 of the License, or (at 
# your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
# USA

#### End of file mkt ####
